package cn.mrlong.segmentcontrol;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;


public class SegmentControl extends LinearLayout implements View.OnClickListener {

    private String[] titles = new String[]{};
    private int textColor = 0;
    private int selectedTextColor = 0;
    private int textSize = 16;
    private OnSegmentListener listener;
    private List<TextView> viewList = new ArrayList<>();

    public void setOnSegmentListener(OnSegmentListener listener) {
        if (listener != null)
            this.listener = listener;
    }

    public interface OnSegmentListener {
        public void setOnSegment(View v, int position);
    }

    public SegmentControl(Context context) {
        super(context);
        init();
    }

    public SegmentControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SegmentControl(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        this.removeAllViews();
        this.setPadding(1, 1, 1, 1);
        if (titles.length > 0) {
            //创建子view
            for (int i = 0; i < titles.length; i++) {

                TextView tv_name = new TextView(getContext());

                tv_name.setText(titles[i]);
                setTextColorVal(tv_name);
                tv_name.setTextSize(textSize);
                tv_name.setGravity(Gravity.CENTER);
                //tv_name.setPadding(3, 6, 3, 6);
                tv_name.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
                tv_name.setOnClickListener(this);
                tv_name.setId(i);

                if (i == 0) {
                    tv_name.setBackgroundResource(R.drawable.left);
                    tv_name.setSelected(true);
                    setSelectedTextColor(tv_name);
                    //tv_name.setBackgroundColor(getResources().getColor(R.color.red));
                } else if (i == (titles.length - 1)) {
                    tv_name.setSelected(false);
                    tv_name.setBackgroundResource(R.drawable.rigth);
                } else {
                    tv_name.setSelected(false);
                    tv_name.setBackgroundResource(R.drawable.middle);
                }
                this.addView(tv_name);
                //竖线  去掉最后一个
                if (i != (titles.length - 1)) {
                    TextView line = new TextView(getContext());
                    line.setBackgroundColor(textColor != 0 ? textColor : Color.BLUE);
                    line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
                    this.addView(line);
                }
                viewList.add(tv_name);
            }
        }
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        for (TextView view : viewList) {
            view.setTextSize(this.textSize);
        }
        invalidate();
    }

    public String[] getTitles() {
        return titles;
    }

    public void setSelectedTextColor(int selectedTextColor) {
        this.selectedTextColor = selectedTextColor;
        for (TextView view : viewList) {
            if (view.isSelected())
                setSelectedTextColor(view);
        }
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        for (TextView view : viewList) {
            setTextColorVal(view);
        }
        invalidate();
    }

    /**
     * 设置默认颜色
     *
     * @param view
     */
    private void setTextColorVal(TextView view) {
        view.setTextColor(this.textColor != 0 ? this.textColor : Color.BLUE);
    }

    public void setTitles(String[] titles) {
        this.titles = titles;
        init();
        invalidate();
    }

    @Override
    public void onClick(View v) {
        for (TextView view : viewList) {
            view.setSelected(false);
            setTextColorVal(view);
            //view.setBackgroundColor(getResources().getColor(R.color.white));
        }
        if (this.listener != null) {
            TextView textView = ((TextView) v);
            textView.setSelected(true);
            setSelectedTextColor(textView);
            //textView.setBackgroundColor(getResources().getColor(R.color.red));
            listener.setOnSegment(v, v.getId());
        }
        invalidate();
    }

    /**
     * 设置选中的位置颜色
     *
     * @param textView
     */
    private void setSelectedTextColor(TextView textView) {
        textView.setTextColor(this.selectedTextColor != 0 ? this.selectedTextColor : Color.WHITE);
    }

}
